package net.homelinux.chaoswg.io.renga.core.gui.operations.visualisations;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import net.homelinux.chaoswg.io.renga.core.registries.GlobalPropertyRegistry;

import org.jdom.Element;

public class ImageVisualisation extends Visualisation {
    private BufferedImage image = null;
    private String imageName = null;

    public ImageVisualisation (final String imageName) {
        loadImage(imageName);
        this.imageName = imageName;
        this.area.setLocation(new Point (0,0));
    }

    public ImageVisualisation (final int x, final int y, final String imageName) {
        loadImage(imageName);
        this.imageName = imageName;
        this.area.setLocation(new Point (x,y));    
    }

    public ImageVisualisation (final Point position, final String imageName) {
        loadImage(imageName);
        this.imageName = imageName;
        this.area.setLocation(position);
    }

    public ImageVisualisation (final int x, final int y, final BufferedImage image) {
        this.image = image;

        this.area = new Rectangle (x, y, image.getWidth(), image.getHeight());
    }

    public ImageVisualisation (final Point position, final BufferedImage image) {
        this.image = image;

        this.area = new Rectangle (position, new Dimension (image.getWidth(), image.getHeight()));
    }

    private BufferedImage loadImage (final InputStream istream) {
        BufferedImage img = null;
        try {
            img = ImageIO.read (istream);
        } catch (final Exception excp) {
            System.err.println (excp);
        }

        return img;        
    }

    private void loadImage (final String imageName) {
        InputStream istream = null;

        final File f = new File(imageName);
        if (f.exists()) {
            try {
            istream = new FileInputStream(f);
            } catch (IOException ioexcp) {
                image = null;
            }
        } else {
            istream = ((ClassLoader)GlobalPropertyRegistry.getGlobalPropertyRegistry().getProperty("ClassLoader")).getResourceAsStream("images/" + imageName);
        }

        image = loadImage(istream);
        
        if (image != null) {
            this.area.width = image.getWidth();
            this.area.height = image.getHeight();
        } else {
            this.area.width = 32;
            this.area.height = 32;
        }
    }

    public final void draw(final Graphics g) {
        if (image == null) {
            g.setColor(Color.BLUE);
            g.fillRect(this.area.x, this.area.y, this.area.width, this.area.height);
            g.setColor(Color.BLACK);
        } else {
            g.drawImage(image, this.area.x, this.area.y, null);
        }
    }

    public Element writeYourself () {
        final Element toReturn = super.writeYourself();
        if (imageName != null) {
            final Element imageNameElement = new Element ("image");

            imageNameElement.setText(imageName);

            toReturn.addContent(imageNameElement);
        }

        return toReturn;
    }
}
